package main

import (
	"chaincode/api"
	"fmt"
	"time"

	"github.com/hyperledger/fabric/core/chaincode/shim"
	pb "github.com/hyperledger/fabric/protos/peer"
)

type BlockChainItem struct {
}

// Init 链码初始化
func (t *BlockChainItem) Init(stub shim.ChaincodeStubInterface) pb.Response {
	fmt.Println("链码初始化")
	// //初始化默认数据
	// var accountIds = [1]string{
	// 	"114514",
	// }
	// var userNames = [1]string{"管理员"}
	// var balances = [1]float64{0}
	// //初始化账号数据
	// for i, val := range accountIds {
	// 	account := &model.Account{
	// 		AccountId: val,
	// 		UserName:  userNames[i],
	// 		Balance:   balances[i],
	// 	}
	// 	// 写入账本
	// 	if err := utils.WriteLedger(account, stub, model.AccountKey, []string{val}); err != nil {
	// 		return shim.Error(fmt.Sprintf("%s", err))
	// 	}
	// }
	return shim.Success(nil)
}

// Invoke 实现Invoke接口调用智能合约
func (t *BlockChainItem) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	funcName, args := stub.GetFunctionAndParameters()
	switch funcName {
	case "updAccountList":
		fmt.Println("---------------------------updAccountList-----------------------------")
		return api.UpdAccountList(stub, args)
	case "updItem":
		return api.UpdItem(stub, args)
	case "hello":
		return api.Hello(stub, args)
	case "queryAccountList":
		return api.QueryAccountList(stub, args)
	case "createItem":
		return api.CreateItem(stub, args)
	case "queryItem":
		return api.QueryItem(stub, args)
	case "createSelling":
		return api.CreateSelling(stub, args)
	case "querySellingList":
		return api.QuerySellingList(stub, args)
	case "querySellingListByBuyer":
		return api.QuerySellingListByBuyer(stub, args)
	case "updateSelling":
		return api.UpdateSelling(stub, args)
	case "createDonating":
		return api.CreateDonating(stub, args)
	case "queryDonatingList":
		return api.QueryDonatingList(stub, args)
	case "queryDonatingListByGrantee":
		return api.QueryDonatingListByGrantee(stub, args)
	case "updateDonating":
		return api.UpdateDonating(stub, args)
	default:
		return shim.Error(fmt.Sprintf("没有该功能: %s", funcName))
	}
}

func main() {
	timeLocal, err := time.LoadLocation("Asia/Shanghai")
	if err != nil {
		panic(err)
	}
	time.Local = timeLocal
	err = shim.Start(new(BlockChainItem))
	if err != nil {
		fmt.Printf("Error starting Simple chaincode: %s", err)
	}
}
